import 'dart:convert';

import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/config/config.dart';
import 'package:yp/pages/trolley/go_exchange/state.dart';
import 'package:yp/services/api.dart';

import '../../../helper.dart';
import '../trolley_logic.dart';

class GoExchangeLogic extends GetxController {
  GoExchangeState state = GoExchangeState();
  var locationResult;
  RxInt cartItemsNum = Get.put(TrolleyLogic()).cartItemsNum;

  @override
  void onInit() {
    super.onInit();
    state.cornerDetail = Get.arguments['cornerDetail'];
    state.reservedNo = Get.arguments['reservedNo'];
    locationResult = SpUtil.getObject(Config.USER_CURRENT_LOCATION);
    print('--------------------------');
    print(Get.arguments);
    print(locationResult);
    print(locationResult['AMapResult']);
    getExchangeList();
    // surroundingSearch(null);
  }

  /// 获取换购列表
  void getExchangeList() {
    // if (locationResult == null && locationResult['defaultAddress'] == null)
    //   return;
    // var defaultAddress = locationResult['defaultAddress'];
    var params = {
      "channel_num_id": 99,
      "use_platform": 2,
      // 'prv_num_id': defaultAddress['p_code'],
      // 'city_num_id': defaultAddress['city_code'],
      // 'city_area_num_id': defaultAddress['ad_code'],
      'access_token': SpUtil.getString('token'),
      'usr_num_id': SpUtil.getInt('empeNumId'),
    };
    // var params = {
    //   "data_sign": 0,
    //   "channel_num_id": 98,
    //   "use_platform": 3,
    //   "prv_num_id": "440000",
    //   "city_num_id": "440300",
    //   "city_area_num_id": "440307",
    //   "access_token": "d97706b5519e49b38f4b72eef9db9025",
    //   "usr_num_id": 1146
    // };
    print(params);
    Api.goExchange(params).listen((event) {
      print(event);
      if (event != null && event['items'] != null) {
        state.exchangeList = event['items'];
      }
      update();
    });
  }

  /// 周边搜索
  void surroundingSearch(String keywords) {
    if (locationResult == null) return;
    var aMapResult = locationResult['AMapResult'];
    Map<String, dynamic> params = {
      "use_platform": 2,
      "maplocation_x": aMapResult["longitude"],
      "maplocation_y": aMapResult["latitude"],
      "city": aMapResult["city"],
      "key_words": keywords
    };
    Api.surroundingSearch(params).listen((event) {
      // print("周边搜索==========$event");
      if (event["code"] == 0 && event["address_list"] != null) {
        List addressList = event["address_list"];
        print('addressList ==> ${addressList.length} ==> $addressList ');
        print('aMapResult ==> $aMapResult');
        if (addressList.length > 0 && locationResult["userAddress"] == null) {
          // locationResult["defaultAddress"] = addressList.first;
          locationResult["defaultAddress"] = addressList.last;
          getExchangeList();
          update();
        }
      }
    });
  }

  // 加入购物车
  void exchangeAddCard({Map<String, dynamic> item}) {
    // if (locationResult == null && locationResult['defaultAddress'] == null)
    //   return;
    // var defaultAddress = locationResult['defaultAddress'];
    var params = {
      "channel_num_id": 99,
      "use_platform": 2,
      "ec_shop_num_id": item['shop_num_id'],
      "item_num_id": item['item_num_id'], // 商品编号
      "ec_style_num_id": item['ec_style_num_id'], // 款式唯一编号
      "style_num_id": item['style_num_id'], // 款式编号
      "grade": item['grade'], // 促销等级
      "type_num_id": item['type_num_id'], // 促销类型
      "usr_num_id": SpUtil.getInt('empeNumId'),
      "promotion_id": item['reserved_no'], // 促销编号
      "quantity": 1,
      "obj_type": "item",
      "mode": "cart",
      'access_token': SpUtil.getString('token'),
      // 'prv_num_id': defaultAddress['p_code'],
      // 'city_num_id': defaultAddress['city_code'],
      // 'city_area_num_id': defaultAddress['ad_code'],
    };
    print('----------------------');
    print(params);
    print(item);
    print('----------------------');
    Api.goExchangeAddCart(params).listen((event) {
      print(event);
      if (event['code'] == 0) {
        // MainAppLogics.getCartNum();
        getCartNum(item['shop_num_id']);
        toast('添加成功');
      } else {
        toast(event['message']);
      }
    });
  }

  getCartNum(shopId) {
    var params2 = {
      "usr_num_id": SpUtil.getInt('empeNumId'),
      "channel_num_id": 99,
      "use_platform": 2,
      'access_token': SpUtil.getString('token'),
      // "shop_id": shopId
    };
    // var params = {
    //   "data_sign": 0,
    //   "channel_num_id": 98,
    //   "use_platform": 3,
    //   "access_token": "2199d64a71b54943b72d1dd47c183fc1",
    //   "shop_id": 102002001
    // };
    // 购物车商品件数
    Api.cartItemNum(params2).listen((event) {
      print('$params2---$event');
      // 购物车不同商品数量同步
      cartItemsNum.value = event["variety"];
      getExchangeList();
      update();
    });
  }
}
